<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" >
      <el-form-item label="考核公司名称" prop="companyName" v-show="showSearch">
<!--        <el-input v-model="queryParams.companyName" placeholder="请输入考核公司名称" clearable style="width: 240px" @keyup.enter.native="handleQuery"/>-->
        <el-select v-model="queryParams.companyName" placeholder="请选择考核公司名称"  @change="handleQuery" style="width: 300px" >
          <el-option v-for="(item, index) in companyOptions" :key="index" :label="item.name"
                     :value="item.name"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item v-show="showSearch">-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
      <!--      <el-form-item >-->
      <!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      <!--      </el-form-item>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
      <el-table v-loading="loading" :data="rowList">
      <el-table-column label="记录单号" align="center" prop="id" width="100" fixed="left"/>
      <el-table-column label="考核日期" align="center" prop="time" width="100" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考核总分" align="center" prop="total" width="120" />
      <el-table-column label="记录单名称" align="center" prop="examinePeople" width="120" />
      <el-table-column label="审核状态" prop="approveStatus" width="120" align="center">
        <template slot-scope="scope">
          <span @click="handleApprove(scope.row)" :style="{'color': colors(scope.row.approveStatus), 'cursor': 'pointer'}">
            {{ scope.row.approveStatus }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="考核公司名称" align="center" prop="companyName" :width="companyNameColWidth" />
      <el-table-column label="考核方案" align="center" prop="planName" :width="planNameColWidth"/>
      <el-table-column label="创建人" align="center" prop="createBy" width="120"/>
      <el-table-column label="修改人" align="center" prop="updateBy" width="120"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >明细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食材配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="考核方案" prop="planId">
          <el-select v-model="form.planId" placeholder="请选择考核方案" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in planOptions" :key="index" :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-select v-model="form.companyName" placeholder="请选择公司" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in companyOptions" :key="index" :label="item.name"
                       :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="记录单名称" prop="examinePeople">
          <el-input v-model="form.examinePeople" placeholder="请输入记录单名称" />
        </el-form-item>
        <el-form-item label="考核时间" prop="time">
           <el-date-picker v-model="form.time" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                          :style="{width: '100%'}" placeholder="请选择记录时间" clearable></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, get, del, add, update, listAll } from "@/api/social/examine/record";
import { listAll as listAllPlan } from "@/api/social/examine/plan";
import { listAllNoOther as listAllCompany } from "@/api/social/examine/company";
import {blurRule, changeRule} from "@/utils/formRules";

export default {
  name: "Record",
  dicts: [],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      rowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      planOptions: [],
      companyOptions: [],
      planNameColWidth: 200,
      companyNameColWidth: 200,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName:'',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planId:changeRule('请选择考核方案'),
        companyName:blurRule('请输入公司名称'),
        examinePeople:blurRule('请输入记录单名称'),
        time:changeRule('请选择考核时间')
      }
    };
  },
  activated() {
    this.getList();
  },
  created() {
    this.getcompany();
  },
  methods: {
    colors(status) {
      if (status === "通过") {
        return '#2a9c4a'
      } else if (status === "拒绝") {
        return '#FF0000'
      } else if (status === "审批中") {
        return '#fe9f29'
      } else {
        return '#fe9f29'
      }
    },
    getcompany(){
      listAllCompany().then(res => {
        this.companyOptions=res.rows
        if(res.rows.length>0){
          this.queryParams.companyName=res.rows[0].name
          this.getList()
        }
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAllPlan().then(res => {
        this.planOptions = [];
        for (var i = 0; i < res.rows.length; i++) {
          this.planOptions.push({
            label: res.rows[i].name,
            value: res.rows[i].id
          });
        }
      });
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        let maxPlanNameWidth = 1;
        let maxCompanyNameWidth = 1;
        for (var i = 0; i < response.rows.length; i++) {
          let row = response.rows[i];
          if (row.planName != null && row.planName.length > maxPlanNameWidth) {
            maxPlanNameWidth =  row.planName.length;
          }
          if (row.companyName != null && row.companyName.length > maxCompanyNameWidth) {
            maxCompanyNameWidth =  row.companyName.length;
          }
        }
        this.planNameColWidth = this.planNameColWidth > maxPlanNameWidth * 20 ? this.planNameColWidth : maxPlanNameWidth * 20;
        this.companyNameColWidth = this.companyNameColWidth > maxCompanyNameWidth * 20 ? this.companyNameColWidth : maxCompanyNameWidth * 20;
        this.rowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleApprove(row) {
      if (row.approveStatus === "未提交审批") {
        this.$modal.msgWarning("未提交审批");
      } else {
        this.$router.push("/approve/detail/" + row.approveMissionId);
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {};
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除记录编号为"' + row.id + '"的数据项？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handleDetail":
          this.handleDetail(row);
          break;
        default:
          break;
      }
    },
    handleDetail: function(row) {
      const id = row.id;
      this.$router.push("/social/examine/record/detail/" + id);
    }
  }
};
</script>
